<template>
  <div class="h100 withMinHeightTbodyBox" ref="strategyTableList">
    <s-table
      :columns="columns"
      :data="loadData"
      :scrollX="900"
      row-key="id"
      class="h100"
      ref="table"
    >
      <template slot-scope="text" v-for="column in commonColumns" :slot="column.scopedSlots.customRender">
        <table-ellipsis :text="text" :key="column.dataIndex"></table-ellipsis>
      </template>
      <template slot="name" slot-scope="text,record">
        <auth-text
          @click="doAction('lookupRowDetail', record)"
          :auth="strategyPerms[versionType][upgradeTriggerType].fotaSjcl.view"
        >
          <table-ellipsis :text="text"></table-ellipsis>
        </auth-text>
      </template>
      <template slot="vehicleVersion" slot-scope="text,record">
        <a @click="showVehicleVersionDetail(record)">{{text}}</a>
      </template>
      <template slot="relatedECU" slot-scope="text,record">
        <auth-text
          v-if="text"
          @click="doAction('lookupRowDetail', record)"
          :auth="strategyPerms[versionType][upgradeTriggerType].fotaSjcl.view"
        >
          <i class="iconfont icon-ecu"></i>
          <span style="font-weight:bolder">{{ text }}</span>
        </auth-text>
        <div v-else>
          <i class="iconfont icon-ecu"></i>
          <span>0</span>
        </div>
      </template>
      <template slot="status" slot-scope="text,record">
        <table-ellipsis :text="record.enabled === false ? $t('stoped') : (strategyStatusObj[text] || '-')">
        </table-ellipsis>
      </template>
      <!-- <template slot="publishTime" slot-scope="text,record">
        <table-ellipsis :text="record.status === 9 && $commonFun.getDateMinuteStr(text) || '-'"></table-ellipsis>
      </template> -->
      <template slot="createTime" slot-scope="text">
        <table-ellipsis :text="$commonFun.getDateTimeStr(text)"></table-ellipsis>
      </template>
      <template slot="action" slot-scope="text,record" v-if="versionType !== 'develop'">
        <!-- <operation-menu
          :menus="getTbRowMenus(record)"
          @clickaction="(key) => {
            doAction(key, record)
          }"
        ></operation-menu> -->
        <span style="display: flex;">
          <auth-button style="margin-right:8px" v-if="!(record.status === 1)" @click="doAction('lookupTest', record)" type="primary" :ghost="true" size="small" :auth="strategyPerms[versionType][upgradeTriggerType].fotaSjcl.viewTest">
            <i class="iconfont icon-yanjing"/>查看测试</auth-button>
          <template v-if="!queryParams.fromGcy">
            <auth-button style="margin-right:8px" v-if="!(record.status !== 1 || !record.enabled)" @click="doAction('editStrategy', record)" type="primary" :ghost="true" size="small" :auth="strategyPerms[versionType][upgradeTriggerType].fotaSjcl.edit" >
              <i class="iconfont icon-xiugai"/>编辑</auth-button>

            <auth-button style="margin-right:8px" v-if="!(record.enabled && record.status !== 1)" @click="doAction('deleteList', record)" type="danger" :ghost="true" size="small" :auth="strategyPerms[versionType][upgradeTriggerType].fotaSjcl.delete" >
              <i class="iconfont icon-shanchu3 deleteUlr"/>删除</auth-button>

            <auth-button style="margin-right:8px" v-if="!(!record.enabled || record.status === 6 || record.status === 9)" @click="doAction('createTest', record)" type="primary" :ghost="true" size="small" :auth="strategyPerms[versionType][upgradeTriggerType].fotaSjcl.createTest" >

              <a-icon type="plus-square"/>新建测试</auth-button>

            <auth-button style="margin-right:8px" :disabled="!(versionType === 'channel' && record.enabled && record.status === 2 && record.versionType === 'PROD')" @click="doAction('publishStrategy', record)" type="primary" :ghost="true" size="small" :auth="strategyPerms[versionType][upgradeTriggerType].fotaSjcl.publish">
              发布策略</auth-button>

            <auth-button style="margin-right:8px" v-if="record.enabled" @click="doAction('disabledStrategy', record)" type="danger" :ghost="true" size="small" :auth="strategyPerms[versionType][upgradeTriggerType].fotaSjcl.disabled">
              <a-icon type="stop"/>停用</auth-button>

            <auth-button style="margin-right:8px" v-if="!record.enabled" @click="doAction('enabledStrategy', record)" type="primary" :ghost="true" size="small" :auth="strategyPerms[versionType][upgradeTriggerType].fotaSjcl.disabled">
              <a-icon type="check"/>启用</auth-button>

            <auth-button @click="doAction('copyStrategy', record)" type="primary" :ghost="true" size="small" :auth="strategyPerms[versionType][upgradeTriggerType].fotaSjcl.copy">
              <i class="iconfont icon-fuzhi"/>复制</auth-button>
            </template>
        </span>
      </template>
    </s-table>
    <!-- 查看车辆版本详情 -->
    <custom-modal
      :width="1000"
      :visible="vehicleVersionVisible"
      :title="vehicleVersionInfo.title"
      :destroyOnClose="true"
      @cancel="handleCancelVehicleVersion"
      :isOkBtn="false"
      class="largeVersionPopup"
      modalCls=".largeVersionPopup"
      :bodyStyle="{ height: '500px', overflowY: 'auto'}">
      <VehicleVersionDetail :id="vehicleVersionInfo.vehicleVersionId" :versionType="vehicleVersionInfo.versionType"/>
    </custom-modal>
  </div>
</template>
<script>
import { strategyTbColumns, strategyTbRowAction } from '../config/list'
import tableEllipsis from '@/components/Ellipsis/TableEllipsis'
import OperationMenu from '@/components/OperationMenu'
import strategyApi from '@/api/modules/strategy'
import { mapState } from 'vuex'
import { STable } from '@/components'
import { strategyPerms } from '@/config/permission/index'
import AuthText from '@/components/AuthText'
import AuthButton from '@/components/AuthButton'
import VehicleVersionDetail from '@views/software/vehicleVersion/components/VehicleVersionDetail'
import CustomModal from '@/components/CustomModal/index.vue'
export default {
  components: {
    tableEllipsis,
    STable,
    OperationMenu,
    AuthText,
    AuthButton,
    VehicleVersionDetail,
    CustomModal
  },
  props: {
    //实为 testType，为保持兼容复用原参数
    versionType: {
      type: String,
      default: ''
    },
    upgradeTriggerType: {
      type: String,
      default: ''
    },
    queryParams: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  inject: ['queryParams'],
  computed: {
    ...mapState({
      // queryParams: (state) => state.strategy.queryParams,
      strategyStatus: (state) => state.dict.strategyStatusDictCode
    }),
    commonColumns () {
      return this.columns.filter(item => !item.special)
    },
    strategyStatusObj () {
      return this.$store.getters.getDictCodeObj('strategyStatusDictCode')
    }
  },
  data () {
    let cols = strategyTbColumns;
    if (this.versionType === 'develop') {
      cols = cols.filter(value => value.dataIndex !== 'action');
    }
    return {
      vehicleVersionVisible:false,
      vehicleVersionInfo:{},
      strategyPerms,
      tbRowMenus: strategyTbRowAction,
      columns: cols
    };
  },
  created () {
    this.getPageDictData()
  },
  mounted () {
  },
  methods: {
    // 查看车辆版本详情
    showVehicleVersionDetail (record) {
      this.vehicleVersionInfo = record
      this.vehicleVersionVisible = true
      this.vehicleVersionInfo.title = this.$t('smartDrivingDomainVersion') + this.$t('detail')
      if (this.vehicleVersionInfo.versionType === 'TEST') {
        this.vehicleVersionInfo.title += ' 【测试版】';
      } else {
        this.vehicleVersionInfo.title += ' 【正式版】';
      }
    },
    handleCancelVehicleVersion(){
      this.vehicleVersionVisible = false
    },
    loadData (parameter) {
      return strategyApi.getStrategyPageData({ ...this.queryParams, type: 1, ...parameter }, this.versionType, this.upgradeTriggerType)
    },
    doAction (key, rowData) {
      if (this[key]) {
        this[key](rowData)
        return
      }
      this.$emit('clickbtn', { type: key, data: rowData })
    },
    // 获取操作每行的菜单
    getTbRowMenus (record) {
      // 是否可用
      const arr = this.tbRowMenus.filter(item => {
        return !(item.disabled && item.disabled(record, this))
      })
      // 获取按钮对应的权限
      const arr1 = arr.map(item => {
        const auth = item.getAuth ? item.getAuth(record, this) : ''
        const obj = {
          ...item,
          auth
        }
        delete obj.getAuth
        delete obj.disabled
        return obj
      })
      return arr1
    },
    refreshData (bool=false) {
      this.$refs.table && this.$refs.table.refresh(bool)
    },
    getPageDictData () {
      ['getStrategyStatusDictCodeData'].forEach(method => {
        this.$store.dispatch(method)
      })
    }
  }
}
</script>
